home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / M2Ops.bas < prev    next >
Encoding:
BASIC Source File  |  1999-06-17  |  5.5 KB  |  205 lines

  1. Attribute VB_Name = "M2Ops"
  2. ' Routines for manipulating 2-dimensional
  3. ' vectors and matrices.
  4. Option Explicit
  5.  
  6. ' Create a 2-dimensional identity matrix.
  7. Public Sub m2Identity(M() As Single)
  8. Dim i As Integer
  9. Dim j As Integer
  10.  
  11.     For i = 1 To 3
  12.         For j = 1 To 3
  13.             If i = j Then
  14.                 M(i, j) = 1
  15.             Else
  16.                 M(i, j) = 0
  17.             End If
  18.         Next j
  19.     Next i
  20. End Sub
  21.  
  22. ' Create a translation matrix for translation by
  23. ' distances tx and ty.
  24. Public Sub m2Translate(Result() As Single, _
  25.     ByVal tx As Single, ByVal ty As Single)
  26.  
  27.     m2Identity Result
  28.     Result(3, 1) = tx
  29.     Result(3, 2) = ty
  30. End Sub
  31.  
  32. ' Create a scaling matrix for scaling by factors
  33. ' of sx and sy.
  34. Public Sub m2Scale(Result() As Single, _
  35.     ByVal sx As Single, ByVal sy As Single)
  36.  
  37.     m2Identity Result
  38.     Result(1, 1) = sx
  39.     Result(2, 2) = sy
  40. End Sub
  41.  
  42. ' Create a rotation matrix for rotating by the
  43. ' given angle (in radians).
  44. Public Sub m2Rotate(Result() As Single, ByVal theta As Single)
  45.     m2Identity Result
  46.     Result(1, 1) = Cos(theta)
  47.     Result(1, 2) = Sin(theta)
  48.     Result(2, 1) = -Result(1, 2)
  49.     Result(2, 2) = Result(1, 1)
  50. End Sub
  51. ' Create a rotation matrix that rotates the point
  52. ' (x, y) onto the X axis.
  53. Public Sub m2RotateIntoX(Result() As Single, _
  54.     ByVal X As Single, ByVal Y As Single)
  55. Dim d As Single
  56.  
  57.     m2Identity Result
  58.     d = Sqr(X * X + Y * Y)
  59.     Result(1, 1) = X / d
  60.     Result(1, 2) = -Y / d
  61.     Result(2, 1) = -Result(1, 2)
  62.     Result(2, 2) = Result(1, 1)
  63. End Sub
  64. ' Create a scaling matrix for scaling by factors
  65. ' of sx and sy at the point (x, y).
  66. Public Sub m2ScaleAt(Result() As Single, _
  67.     ByVal sx As Single, ByVal sy As Single, _
  68.     ByVal X As Single, ByVal Y As Single)
  69. Dim T(1 To 3, 1 To 3) As Single
  70. Dim S(1 To 3, 1 To 3) As Single
  71. Dim T_Inv(1 To 3, 1 To 3) As Single
  72. Dim M(1 To 3, 1 To 3) As Single
  73.  
  74.     ' Translate the point to the origin.
  75.     m2Translate T, -X, -Y
  76.  
  77.     ' Compute the inverse translation.
  78.     m2Translate T_Inv, X, Y
  79.  
  80.     ' Scale.
  81.     m2Scale S, sx, sy
  82.  
  83.     ' Combine the transformations.
  84.     m2MatMultiply M, T, S           ' T * S
  85.     m2MatMultiply Result, M, T_Inv  ' T * S * T_Inv
  86. End Sub
  87.  
  88. ' Create a matrix for reflecting across the line
  89. ' passing through (x, y) in direction <dx, dy>.
  90. Public Sub m2ReflectAcross(Result() As Single, _
  91.     ByVal X As Single, ByVal Y As Single, _
  92.     ByVal dx As Single, ByVal dy As Single)
  93. Dim T(1 To 3, 1 To 3) As Single
  94. Dim R(1 To 3, 1 To 3) As Single
  95. Dim S(1 To 3, 1 To 3) As Single
  96. Dim R_Inv(1 To 3, 1 To 3) As Single
  97. Dim T_Inv(1 To 3, 1 To 3) As Single
  98. Dim M1(1 To 3, 1 To 3) As Single
  99. Dim M2(1 To 3, 1 To 3) As Single
  100. Dim M3(1 To 3, 1 To 3) As Single
  101.  
  102.     ' Translate the point to the origin.
  103.     m2Translate T, -X, -Y
  104.  
  105.     ' Compute the inverse translation.
  106.     m2Translate T_Inv, X, Y
  107.  
  108.     ' Rotate so the direction vector lies in the Y axis.
  109.     m2RotateIntoX R, dx, dy
  110.  
  111.     ' Compute the inverse translation.
  112.     m2RotateIntoX R_Inv, dx, -dy
  113.  
  114.     ' Reflect across the X axis.
  115.     m2Scale S, 1, -1
  116.  
  117.     ' Combine the transformations.
  118.     m2MatMultiply M1, T, R     ' T * R
  119.     m2MatMultiply M2, S, R_Inv ' S * R_Inv
  120.     m2MatMultiply M3, M1, M2   ' T * R * S * R_Inv
  121.  
  122.     ' T * R * S * R_Inv * T_Inv
  123.     m2MatMultiply Result, M3, T_Inv
  124. End Sub
  125.  
  126. ' Create a rotation matrix for rotating through
  127. ' angle theta around the point (x, y).
  128. Public Sub m2RotateAround(Result() As Single, _
  129.     ByVal theta As Single, _
  130.     ByVal X As Single, ByVal Y As Single)
  131. Dim T(1 To 3, 1 To 3) As Single
  132. Dim R(1 To 3, 1 To 3) As Single
  133. Dim T_Inv(1 To 3, 1 To 3) As Single
  134. Dim M(1 To 3, 1 To 3) As Single
  135.  
  136.     ' Translate the point to the origin.
  137.     m2Translate T, -X, -Y
  138.  
  139.     ' Compute the inverse translation.
  140.     m2Translate T_Inv, X, Y
  141.  
  142.     ' Rotate.
  143.     m2Rotate R, theta
  144.  
  145.     ' Combine the transformations.
  146.     m2MatMultiply M, T, R
  147.     m2MatMultiply Result, M, T_Inv
  148. End Sub
  149.  
  150. ' Multiply a point and a matrix.
  151. Public Sub m2PointMultiply(ByRef X As Single, ByRef Y As Single, A() As Single)
  152. Dim newx As Single
  153. Dim newy As Single
  154.  
  155.     newx = X * A(1, 1) + Y * A(2, 1) + A(3, 1)
  156.     newy = X * A(1, 2) + Y * A(2, 2) + A(3, 2)
  157.     X = newx
  158.     Y = newy
  159. End Sub
  160. ' Set copy = orig.
  161. Public Sub m2PointCopy(copy() As Single, orig() As Single)
  162. Dim i As Integer
  163.  
  164.     For i = 1 To 3
  165.         copy(i) = orig(i)
  166.     Next i
  167. End Sub
  168.  
  169.  
  170. ' Set copy = orig.
  171. Public Sub m2MatCopy(copy() As Single, orig() As Single)
  172. Dim i As Integer
  173. Dim j As Integer
  174.  
  175.     For i = 1 To 3
  176.         For j = 1 To 3
  177.             copy(i, j) = orig(i, j)
  178.         Next j
  179.     Next i
  180. End Sub
  181.  
  182.  
  183.  
  184. ' Apply a transformation matrix to a point.
  185. Public Sub m2Apply(V() As Single, A() As Single, Result() As Single)
  186.     Result(1) = V(1) * A(1, 1) + V(2) * A(2, 1) + A(3, 1)
  187.     Result(2) = V(1) * A(1, 2) + V(2) * A(2, 2) + A(3, 2)
  188.     Result(3) = 1#
  189. End Sub
  190.  
  191.  
  192. ' Multiply two transformation matrices.
  193. Public Sub m2MatMultiply(Result() As Single, A() As Single, B() As Single)
  194.     Result(1, 1) = A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1)
  195.     Result(1, 2) = A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2)
  196.     Result(1, 3) = 0#
  197.     Result(2, 1) = A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1)
  198.     Result(2, 2) = A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2)
  199.     Result(2, 3) = 0#
  200.     Result(3, 1) = A(3, 1) * B(1, 1) + A(3, 2) * B(2, 1) + B(3, 1)
  201.     Result(3, 2) = A(3, 1) * B(1, 2) + A(3, 2) * B(2, 2) + B(3, 2)
  202.     Result(3, 3) = 1#
  203. End Sub
  204.  
  205.